import {
  projectCode,
  byCode,
  getRegular,
  getRegularActive,
  sendCode,
  resetPassword,
  resetPasswordAdmin,
  checkedPhone,
  checkedAccount
} from '../services/account';

export default {
  namespace: 'account2',
  state: {
    data: [],
    projects: [],
    loading: true,
    rdata: {},
  },
  effects: {
    *projectCode({ _, callback }, { call, put }) {
      const response = yield call(projectCode);
      yield put({
        type: 'save',
        payload: { projects: response },
      });
      if (callback) callback(response);
    },
    *byCode({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(byCode, payload);
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
    *getRegular({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(getRegular, payload);
      yield put({
        type: 'save',
        payload: { rdata: response, loading: false },
      });
      if (callback) callback(response);
    },
    *getRegularActive({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(getRegularActive, payload);
      yield put({
        type: 'save',
        payload: { rdata: response, loading: false },
      });
      if (callback) callback(response);
    },
    *sendCode({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(sendCode, payload);
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
    *checkedAccount({ payload, callback }, { call, put }) {
      const response = yield call(checkedAccount, payload);
      return Promise.resolve(response)
    },
    *checkedPhone({ payload, callback }, { call }) {
      const response = yield call(checkedPhone, payload);
      if (callback) callback(response);
    },
    *resetPassword({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(resetPassword, payload);
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
    *resetPasswordAdmin({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { loading: true },
      });
      const response = yield call(resetPasswordAdmin, payload);
      yield put({
        type: 'save',
        payload: { loading: false },
      });
      if (callback) callback(response);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
